package com.it.dao;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.it.domain.BlogEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @author : code1997
 * @date : 2021/9/9 22:46
 */
@Mapper
public interface BolgMapper extends BaseMapper<BlogEntity> {

    @Select("select b from t_blog b where b.recommend=true")
    List<BlogEntity> getTop();

    @Select("select b.* from t_blog b order by views limit #{size}")
    List<BlogEntity> listRecommedBlogTop(@Param("size") Integer size);


    @Select("select b from t_blog b where b.title like #{query} or b.content like #{query}")
    Page<BlogEntity> findByQuery(@Param("query") String query);

    @Update("update t_blog b set  b.views=b.views+1 where b.id=#{id}")
    int updateViews(@Param("id") Long id);


    @Select("select DATE_FORMAT(b.update_time,'%Y') as year from t_blog b group by year order by year desc")
    List<String> findGroupYear();

    @Select("select b.* from t_blog b where DATE_FORMAT(b.update_time,'%Y')=#{year}")
    List<BlogEntity> findByYear(@Param("year") String year);


}
